package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Academy;
import model.Subject;
import model.beans.SubtopicBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import business.NivelConocimiento;
import business.Subtemas;
import business.Temas;
import business.Unidades;
import business.misc.Util;

public class SubtopicAction extends MappingDispatchAction {
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	
    	int int_topicId = Integer.parseInt(request.getParameter("topic_id"));
    	int int_unitId = Integer.parseInt(request.getParameter("unit_id"));    	    	
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode ,str_subjectCode ));
    	request.setAttribute("tema", Temas.instance(int_topicId, str_academyCode, str_subjectCode ));

    	request.setAttribute("lst_subtemas", Subtemas.list(int_topicId, int_unitId, str_academyCode, str_subjectCode));
    	
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		DynaActionForm forma = (DynaActionForm)form;
		forma.initialize(mapping);
		forma.reset(mapping, request);
		
		int int_topicId = Integer.parseInt(request.getParameter("topic_id"));
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode , str_subjectCode)); 
    	request.setAttribute("tema", Temas.instance(int_topicId, str_academyCode, str_subjectCode ));
    	
    	request.setAttribute("niveles", NivelConocimiento.lista(this.getResources(request)));
    	
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;

		int int_subtopicId = Integer.parseInt(request.getParameter("subtopic_id"));
		int int_topicId = Integer.parseInt(request.getParameter("topic_id"));
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode , str_subjectCode)); 
		request.setAttribute("tema", Temas.instance(int_topicId, str_academyCode, str_subjectCode  ));
		
		request.setAttribute("niveles", NivelConocimiento.lista(this.getResources(request)));
		
		DynaActionForm forma = (DynaActionForm)form;
		SubtopicBean bn_subtopic = business.Subtemas.instance(int_subtopicId);
		bn_subtopic.setAcademy(str_academyCode);
		bn_subtopic.setSubject(str_subjectCode);
		forma.set("int_subtopicId", new Integer(bn_subtopic.getInt_subtopicId()));
		forma.set("int_subtopicNumber", new Integer(bn_subtopic.getInt_subtopicNumber()));
		forma.set("str_descripcion", bn_subtopic.getStr_descripcion());
		forma.set("int_topicId", new Integer(bn_subtopic.getInt_topicId()));
		forma.set("str_objetivos",bn_subtopic.getStr_objetivos());
		forma.set("objetivoAlumno", bn_subtopic.getObjetivoAlumno());
		forma.set("ejecucionAceptable", bn_subtopic.getEjecucionAceptable());
		forma.set("ejecucionEspecifica", bn_subtopic.getEjecucionEspecifica());
		forma.set("experienciaAprendizaje", bn_subtopic.getExperienciaAprendizaje());
		forma.set("grupo", new Integer( bn_subtopic.getGrupo() ) );
		forma.set("numeroCarta", bn_subtopic.getNumeroCarta());
		forma.set("material", bn_subtopic.getMaterial());
		forma.set("nivelConocimiento", bn_subtopic.getNivelConocimiento());
		forma.set("procedimientoEvaluar", bn_subtopic.getProcedimientoEvaluar());
		forma.set("producto", bn_subtopic.getProducto());
		forma.set("tiempoMinimo", new Integer( bn_subtopic.getTiempoMinimo() ) );
		forma.set("tipo", bn_subtopic.getTipo());
		
		forma.set("objetivoAlumno2", bn_subtopic.getObjetivoAlumno2());
		forma.set("ejecucionAceptable2", bn_subtopic.getEjecucionAceptable2());
		forma.set("ejecucionEspecifica2", bn_subtopic.getEjecucionEspecifica2());
		forma.set("experienciaAprendizaje2", bn_subtopic.getExperienciaAprendizaje2());
		forma.set("grupo2", new Integer( bn_subtopic.getGrupo2() ) );
		forma.set("numeroCarta2", bn_subtopic.getNumeroCarta2());
		forma.set("material2", bn_subtopic.getMaterial2());
		forma.set("nivelConocimiento2", bn_subtopic.getNivelConocimiento2());
		forma.set("procedimientoEvaluar2", bn_subtopic.getProcedimientoEvaluar2());
		forma.set("producto2", bn_subtopic.getProducto2());
		forma.set("tiempoMinimo2", new Integer( bn_subtopic.getTiempoMinimo2() ) );
		forma.set("tipo2", bn_subtopic.getTipo2());
		
		forma.set("objetivoAlumno3", bn_subtopic.getObjetivoAlumno3());
		forma.set("ejecucionAceptable3", bn_subtopic.getEjecucionAceptable3());
		forma.set("ejecucionEspecifica3", bn_subtopic.getEjecucionEspecifica3());
		forma.set("experienciaAprendizaje3", bn_subtopic.getExperienciaAprendizaje3());
		forma.set("grupo3", new Integer( bn_subtopic.getGrupo3() ) );
		forma.set("numeroCarta3", bn_subtopic.getNumeroCarta3());
		forma.set("material3", bn_subtopic.getMaterial3());
		forma.set("nivelConocimiento3", bn_subtopic.getNivelConocimiento3());
		forma.set("procedimientoEvaluar3", bn_subtopic.getProcedimientoEvaluar3());
		forma.set("producto3", bn_subtopic.getProducto3());
		forma.set("tiempoMinimo3", new Integer( bn_subtopic.getTiempoMinimo3() ) );
		forma.set("tipo3", bn_subtopic.getTipo3());
		
		forma.set("objetivoAlumno4", bn_subtopic.getObjetivoAlumno4());
		forma.set("ejecucionAceptable4", bn_subtopic.getEjecucionAceptable4());
		forma.set("ejecucionEspecifica4", bn_subtopic.getEjecucionEspecifica4());
		forma.set("experienciaAprendizaje4", bn_subtopic.getExperienciaAprendizaje4());
		forma.set("grupo4", new Integer( bn_subtopic.getGrupo4() ) );
		forma.set("numeroCarta4", bn_subtopic.getNumeroCarta4());
		forma.set("material4", bn_subtopic.getMaterial4());
		forma.set("nivelConocimiento4", bn_subtopic.getNivelConocimiento4());
		forma.set("procedimientoEvaluar4", bn_subtopic.getProcedimientoEvaluar4());
		forma.set("producto4", bn_subtopic.getProducto4());
		forma.set("tiempoMinimo4", new Integer( bn_subtopic.getTiempoMinimo4() ) );
		forma.set("tipo4", bn_subtopic.getTipo4());
		
		forma.set("objetivoAlumno5", bn_subtopic.getObjetivoAlumno5());
		forma.set("ejecucionAceptable5", bn_subtopic.getEjecucionAceptable5());
		forma.set("ejecucionEspecifica5", bn_subtopic.getEjecucionEspecifica5());
		forma.set("experienciaAprendizaje5", bn_subtopic.getExperienciaAprendizaje5());
		forma.set("grupo5", new Integer( bn_subtopic.getGrupo5() ) );
		forma.set("numeroCarta5", bn_subtopic.getNumeroCarta5());
		forma.set("material5", bn_subtopic.getMaterial5());
		forma.set("nivelConocimiento5", bn_subtopic.getNivelConocimiento5());
		forma.set("procedimientoEvaluar5", bn_subtopic.getProcedimientoEvaluar5());
		forma.set("producto5", bn_subtopic.getProducto5());
		forma.set("tiempoMinimo5", new Integer( bn_subtopic.getTiempoMinimo5() ) );
		forma.set("tipo5", bn_subtopic.getTipo5());
				
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		DynaActionForm forma = (DynaActionForm)form;
		String str_forward;
		
		int int_topicId = Integer.parseInt(request.getParameter("topic_id"));
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));		
		request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode , str_subjectCode)); 
		
		if( ((Integer)forma.get("int_subtopicId")).intValue() == 0 ){
			if( business.Subtemas.add(forma.getMap(), int_topicId) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}else{
			if( business.Subtemas.edit(forma.getMap()) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}
		
		if(str_forward.equals(Util.SUCCESS)){
	    	request.setAttribute("lst_subtemas", Subtemas.list(int_topicId, int_unitId, str_academyCode, str_subjectCode));
			request.setAttribute("tema", Temas.instance(int_topicId, str_academyCode, str_subjectCode  ));
		}
		return mapping.findForward(str_forward); 
	
}
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		int int_subtopicId = Integer.parseInt(request.getParameter("subtopic_id"));
		int int_topicId =Integer.parseInt(request.getParameter("topic_id"));
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");		
		
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));		
		request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode ,str_subjectCode ));
		request.setAttribute("tema", Temas.instance(int_topicId, str_academyCode, str_subjectCode  ));
		
		if( business.Subtemas.delete(int_subtopicId) > 0 ){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("Subtema.errors.FailDelete"));
		}		
		request.setAttribute("lst_subtemas", Subtemas.list(int_topicId, int_unitId, str_academyCode, str_subjectCode));
		
		return mapping.findForward(Util.DEFAULT);
	}

}
